package com.sys.web.back.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.common.pojo.ApiResponse;
import com.sys.common.servlet.BaseServlet;
import com.sys.common.util.PageInfoUtil;
import com.sys.orders.pojo.TOrders;
import com.sys.orders.pojo.TOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.orders.service.impl.OrdersDbServiceImpl;
import com.sys.users.service.IUsersDbService;
import com.sys.users.service.impl.UsersDbServiceImpl;
import com.sys.wares.service.IGoodsDbService;
import com.sys.wares.service.impl.GoodsDbServiceImpl;

/**
 * 订单操作
 * 
 * @author 杨志超
 */
@WebServlet(urlPatterns = "/back/OrdersBackServlet")
public class OrdersBackServlet extends BaseServlet
{
	private IOrdersDbService ordersDbService;
	private IGoodsDbService goodsDbService;
	private IUsersDbService usersDbService;

	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.usersDbService = new UsersDbServiceImpl();
		this.goodsDbService = new GoodsDbServiceImpl();
		
		OrdersDbServiceImpl ordersDbServiceImpl = new OrdersDbServiceImpl();
		ordersDbServiceImpl.setGoodsDbService(goodsDbService);
		ordersDbServiceImpl.setUsersDbService(usersDbService);
		
		this.ordersDbService = ordersDbServiceImpl ; 
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 接收参数 */
		String method = request.getParameter("method");
		if ("ordersList".equalsIgnoreCase(method))
		{
			/* ordersList */
			ordersList(request, response);
		} else if ("ordersUpdate".equalsIgnoreCase(method))
		{
			/* ordersUpdate */
			ordersUpdate(request, response);
		} else if ("ordersGoodsList".equalsIgnoreCase(method))
		{
			/* ordersGoodsList */
			ordersGoodsList(request, response);
		} else if ("ordersGoodsUpdate".equalsIgnoreCase(method))
		{
			/* ordersGoodsUpdate */
			ordersGoodsUpdate(request, response);
		} 
	}
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		String path = this.prefix + "/back/orders/ordersList" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	public void ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/back/orders/ordersInfo" ;
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<TOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			path =  "/back/orders/ordersUpdate";
		}
		/* 页面跳转 */
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void ordersGoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		condMap.put("extend", "true");
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<TOrdersGoods> apiResponse = this.ordersDbService.findCondListOrdersGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		String path = this.prefix + "/back/orders/ordersGoodsList" + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	public void ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/back/orders/ordersGoodsInfo" ; 
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		
		condMap.put("extend", "true");
		ApiResponse<TOrdersGoods> apiResponse = this.ordersDbService.findOneOrdersGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			path = "/back/orders/ordersGoodsUpdate";
		}
		path = this.prefix + path + this.subfix; 
		request.getRequestDispatcher(path).forward(request, response);
	}
}